﻿Ext.define('Html5App.controller.ReportController', {
	extend : 'Ext.app.Controller',
	config : {
		models : [],
		views : [],
		refs : {
			customerList : 'customerList',
			customerAddressList : 'customerAddressList',
			selectCustomer : 'button[action=selectCustomer]',
			selectCustomerAddress : 'button[action=selectCustomerAddress]',
			reportOrder : 'reportOrder',
			reportSales : 'reportSales',
			reportLocal : 'reportLocal'
		},
		control : {
			customerList : {
				show : function(element, eOpts) {
					Ext.getCmp('customer-list-data').add(
						{
							xtype : 'button',
							cls : 'customer-options-button',
							style : 'color:#FFFFFF;',
							html : '數據加載...'
						}
					);
					util.showLoadMask(null);
					var customerList;
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select * from Customer where SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				customerList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
					setTimeout(function dataShow(){
						Ext.getCmp('customer-list-data').removeAll();
						Ext.getCmp('customer-list-data').add(
							{
								xtype : 'button',
								cls : 'customer-options-button',
								style : 'color:#FFFFFF;',
								action : 'selectCustomer',
								data : '',
								html : '全部'
							}
						);
						if(customerList && Number(customerList.length)>0){
							Ext.getCmp('customer-list-data').add(
								{
									cls : 'customer-options-panel-line'
								}
							);
							for(var i=0;i<customerList.length;i++){
								var customerObj = customerList.item(i);
								Ext.getCmp('customer-list-data').add(
									{
										xtype : 'button',
										cls : 'customer-options-button',
										style : 'color:#FFFFFF;',
										action : 'selectCustomer',
										data : customerObj["CustomerCode"],
										html : customerObj["CustomerCode"]+' '+customerObj["CustomerName"]
									}
								);
								if(Number(i)<Number(customerList.length-1)){
									Ext.getCmp('customer-list-data').add(
										{
											cls : 'customer-options-panel-line'
										}
									);
								}
							}
						}
						util.hideLoadMask();
					},1500);
				}
			},
			customerAddressList : {
				show : function(element, eOpts) {
					Ext.getCmp('customer-address-list-data').add(
						{
							xtype : 'button',
							cls : 'customer-options-button',
							style : 'color:#FFFFFF;',
							html : '數據加載...'
						}
					);
					var CustomerCode = "";
					if(Ext.operationSource == 'orderRecord'){
						Ext.getCmp("options_panel_triangle").setCls('report-customer-address-list-options-panel-triangle');
						CustomerCode = config.customer_code.replace("customer_button_","").split("_")[0];
					}else if(Ext.operationSource == 'reportOrder'){
						Ext.getCmp("options_panel_triangle").setCls('report-customer-list-options-panel-triangle');
						if(Ext.getCmp("order-record-customer-label")){
							CustomerCode = Ext.getCmp("order-record-customer-label").getData();
						}
					}
					util.showLoadMask(null);
					var customerAddressList;
					config.database.transaction(
				        function (tx) {
				            tx.executeSql(
				        		"select tmp.* from ( "+
								" select CustomerCode, '001' as IDCUSTSHPT, "+
								" rtrim(ltrim(Address1)) as Address1, rtrim(ltrim(Address2)) as Address2, rtrim(ltrim(Address3)) as Address3, '' as Address4 "+
								" from Customer where CustomerCode='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"' "+
								" union all "+
								" select IDCUST as CustomerCode, IDCUSTSHPT, "+
								" rtrim(ltrim(TEXTSTRE1)) as Address1, rtrim(ltrim(TEXTSTRE2)) as Address2, rtrim(ltrim(TEXTSTRE3)) as Address3, rtrim(ltrim(TEXTSTRE4)) as Address4 "+
								" from CustomerAddress where IDCUST='"+CustomerCode+"' and SalesCode='"+config.current_user.user_name+"' "+
								" ) tmp order by tmp.CustomerCode, tmp.IDCUSTSHPT", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				customerAddressList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
				    setTimeout(function dataShow(){
				    	Ext.getCmp('customer-address-list-data').removeAll();
				    	Ext.getCmp('customer-address-list-data').add(
							{
								xtype : 'button',
								cls : 'customer-options-button',
								style : 'color:#FFFFFF;',
								action : 'selectCustomerAddress',
								data : '~~~',
								html : '選擇送貨地址'
							}
						);
				    	if(customerAddressList  && Number(customerAddressList.length)>0){
				    		Ext.getCmp('customer-address-list-data').add(
								{
									cls : 'customer-options-panel-line'
								}
							);
							for(var i=0;i<customerAddressList.length;i++){
								var num = (i+1);
                    			num = num < 10 ? '0'+num : num;
								var customerAddressObj = customerAddressList.item(i);
								Ext.getCmp('customer-address-list-data').add(
									{
										xtype : 'button',
										cls : 'customer-options-button',
										style : 'color:#FFFFFF;',
										action : 'selectCustomerAddress',
										data : customerAddressObj["IDCUSTSHPT"]+" "+customerAddressObj["Address1"]+" "+customerAddressObj["Address2"]+" "+customerAddressObj["Address3"]+" "+customerAddressObj["Address4"],
										html : customerAddressObj["IDCUSTSHPT"]+" "+customerAddressObj["Address1"]+" "+customerAddressObj["Address2"]+" "+customerAddressObj["Address3"]+" "+customerAddressObj["Address4"]
									}
								);
								if(Number(i)<Number(customerAddressList.length-1)){
									Ext.getCmp('customer-address-list-data').add(
										{
											cls : 'customer-options-panel-line'
										}
									);
								}
							}
				    	}
				    	util.hideLoadMask();
				    },1500);
				}
			},
			selectCustomer : {
				tap : function(button, e, eOpts) {
					if(Ext.getCmp("sales-progress-customer-label")){
						Ext.getCmp("sales-progress-customer-label").setHtml(button.getHtml());
						Ext.getCmp("sales-progress-customer-label").setData(button.getData());
						if(Ext.getCmp('queryRadioDate').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.salesProgressView(0);
                    	}else if(Ext.getCmp('queryRadioMonth').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.salesProgressView(1);
                    	}						
					}else if(Ext.getCmp("order-record-customer-label")){
						Ext.getCmp("order-record-customer-address").setHtml('選擇送貨地址');
						Ext.getCmp("order-record-customer-address").setData('~~~');
						Ext.getCmp("order-record-customer-label").setHtml(button.getHtml());
						Ext.getCmp("order-record-customer-label").setData(button.getData());
						if(Ext.getCmp('RadioDate1').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.orderRecordReportView(1);
                    	}else if(Ext.getCmp('RadioDate2').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.orderRecordReportView(2);
                    	}else if(Ext.getCmp('RadioDate3').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.orderRecordReportView(3);
                    	}
					}
					button.parent.parent.destroy();
				}
			},
			selectCustomerAddress : {
				tap : function(button, e, eOpts) {
					if(Ext.operationSource == 'orderRecord' || Ext.operationSource == 'reportOrder'){
						Ext.getCmp("order-record-customer-address").setHtml(button.getHtml());
						Ext.getCmp("order-record-customer-address").setData(button.getData());
						if(Ext.getCmp('RadioDate1').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.orderRecordReportView(1);
                    	}else if(Ext.getCmp('RadioDate2').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.orderRecordReportView(2);
                    	}else if(Ext.getCmp('RadioDate3').getSrc().indexOf('radio_checked.png')>-1){
                    		custom.orderRecordReportView(3);
                    	}
					}
					button.parent.parent.destroy();
				}
			},
			reportOrder : {
				activate : function(newActiveItem, t, oldActiveItem, eOpts) {
					var now = new Date();
					var curDate = '';
					var curMonth = parseInt(now.getMonth()+1);
				    curDate += now.getFullYear();
				    curDate += '-';
					curDate += curMonth < 10 ? '0'+curMonth : curMonth;
					curDate += '-';
					curDate += now.getDate() < 10 ? '0'+now.getDate() : now.getDate();
					Ext.getCmp("orderRecordBeginDate").getComponent('field-value').setHtml(curDate.substr(0,8)+"01");
					Ext.getCmp("orderRecordBeginDate").getComponent('field-value').setData(curDate.substr(0,8)+"01");
					Ext.getCmp("orderRecordEndDate").getComponent('field-value').setHtml(curDate);
					Ext.getCmp("orderRecordEndDate").getComponent('field-value').setData(curDate);
					config.customer_code = "";
					custom.orderRecordReportView(1);
				}
			},
			reportSales : {
				activate : function(newActiveItem, t, oldActiveItem, eOpts) {
					var now = new Date();
					var curDate = '';
					var curMonth = parseInt(now.getMonth()+1);
				    curDate += now.getFullYear();
				    curDate += '-';
					curDate += curMonth < 10 ? '0'+curMonth : curMonth;
					curDate += '-';
					curDate += now.getDate() < 10 ? '0'+now.getDate() : now.getDate();
					Ext.getCmp("salesProgressBeginDate").getComponent('field-value').setHtml(curDate.substr(0,8)+"01");
					Ext.getCmp("salesProgressBeginDate").getComponent('field-value').setData(curDate.substr(0,8)+"01");
					Ext.getCmp("salesProgressEndDate").getComponent('field-value').setHtml(curDate);
					Ext.getCmp("salesProgressEndDate").getComponent('field-value').setData(curDate);
					Ext.getCmp("salesProgressBeginMonth").getComponent('field-value').setHtml(curDate.substr(0,7));
					Ext.getCmp("salesProgressBeginMonth").getComponent('field-value').setData(curDate.substr(0,7));
					Ext.getCmp("salesProgressEndMonth").getComponent('field-value').setHtml(curDate.substr(0,7));
					Ext.getCmp("salesProgressEndMonth").getComponent('field-value').setData(curDate.substr(0,7));
					config.customer_code = "";
					custom.salesProgressView(0);
				}
			},
			reportLocal : {
				activate : function(newActiveItem, t, oldActiveItem, eOpts) {
					util.showLoadMask(null);
					var orderRecordList,orderRecordDetailList;
					config.database.transaction(
				        function (tx) {
				        	tx.executeSql(
				        		"select orderHead.OrderNo as OrdNumber, " +
							  	" CustomerInfo.CustomerCode as customer2," +
							  	" CustomerInfo.CustomerName as bilname2," +
								" orderHead.OrderDate as OrdDate2," +
								" orderHead.DeliveryDate as DeliveryDate," +
								" case Payment when 1 then '月結' when 2 then '現款' else '其他' end as Payment," +
								" orderHead.DetailCount as DetailCount," +
								" orderHead.TotalAmount as Amt," +
								" (customerAddress.Address1+';'+customerAddress.Address2+';'+customerAddress.Address3+';'+customerAddress.Address4) as ShipTo2" +
								" from FOrderHead orderHead left join (" +
									" select tmp.* from (select CustomerCode,SalesCode, '001' as IDCUSTSHPT, Address1, Address2, Address3, '' as Address4" +
									 " from Customer" +
									 " union all" +
									 " select IDCUST as CustomerCode,SalesCode, IDCUSTSHPT, TEXTSTRE1 as Address1, TEXTSTRE2 as Address2, TEXTSTRE3 as Address3, TEXTSTRE4 as Address4" +
									 " from CustomerAddress" +
									" ) tmp " +
									" where tmp.SalesCode = '"+config.current_user.user_name+"'" +
								" ) customerAddress on orderHead.CustomerCode = customerAddress.CustomerCode and orderHead.Address=CAST(customerAddress.IDCUSTSHPT as int)" +
								" left join (" +
									" select CustomerCode,CustomerName from Customer " +
									" where SalesCode='"+config.current_user.user_name+"'" +
								" ) CustomerInfo on orderHead.CustomerCode = CustomerInfo.CustomerCode" +
								" where " +
								" orderHead.SalesCode='"+config.current_user.user_name+"'", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				            tx.executeSql(
				        		"select detail.OrderNo as OrdNumber, " +
								" detail.DetailNo as DetailNo," +
								" detail.ItemCode as item," +
								" item.Unit as OrdUnit2," +
								" detail.OrderQty as Qty," +
								" detail.Price as UnitPrice2," +
								" detail.SubTotal as Amt," +
								" item.ItemName as desc2 from FOrderDetail detail left join Item item on detail.ItemCode=item.ItemCode " +
								" where item.SalesCode='"+config.current_user.user_name+"' " +
								" and detail.OrderNo in (" +
									" select OrderNo " +
									" from FOrderHead " +
									" where " +
									" SalesCode='"+config.current_user.user_name+"'" +
								" )", 
				        		[],
				        		function (tx, r) {
				        			if(Number(r.rows.length) > 0){
				        				orderRecordDetailList = r.rows;
				        			}
				        		},
				        		function (tx, e) {}
				            );
				        }
				    );
					setTimeout(function dataShow(){
	        			var htmlStr = "";
	            		Ext.getCmp("order-record-data").setHtml(htmlStr);
		                htmlStr += 	'<table border="0" cellpadding="0" cellspacing="0" style="width:40rem;border-top:1px solid #CFCFCF;border-bottom:1px solid #CFCFCF;border-left:1px solid #CFCFCF;font-size:0.7rem;color:#4C4C4C;">'+
									  '<tr style="height:2rem;text-align:center;">'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">訂單號</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">客戶號碼</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:6.5rem;">客戶名稱</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">訂單日期</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4.2rem;">送貨日期</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:3.5rem;">付款方式</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:2.5rem;">項數</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:4rem;">項額</td>'+
									    '<td style="border-bottom:1px solid #CFCFCF;border-right:1px solid #CFCFCF;width:12rem;">送貨地址</td>'+
									  '</tr>';
						config.ord_number_arr = [];
						if(orderRecordList && Number(orderRecordList.length)>0){
							for(var j=0;j<orderRecordList.length;j++){
		            			var orderRecord = orderRecordList.item(j);
		            			config.ord_number_arr.push(orderRecord["OrdNumber"]);
		            			if(j%2==0){
		            				htmlStr += '<tr style="height:2rem;background-color:#F5F5F5;color:#4D4D4D" onclick="javascript:custom.orderToView(\''+orderRecord["OrdNumber"]+'\');">';
		            			}else{
		            				htmlStr += '<tr style="height:2rem;background-color:#FFFFFF;color:#4D4D4D" onclick="javascript:custom.orderToView(\''+orderRecord["OrdNumber"]+'\');">';
		            			}
		                			htmlStr +=  '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["OrdNumber"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["customer2"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["bilname2"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["OrdDate2"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["DeliveryDate"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["Payment"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["DetailCount"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">$'+orderRecord["Amt"]+'</td>'+
											  	'<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecord["ShipTo2"]+'</td>'+
											  '</tr>'+
											  '<tr style="display:none;" id="'+orderRecord["OrdNumber"]+'_Order_Detail">'+
											  	'<td colspan="9">'+
											  		'<table width="100%" border="0" cellpadding="0" cellspacing="0" style="color:#4D4D4D;">'+
											          '<tr style="height:1.6rem;background-color:#E1E1E1;font-weight:bold;text-align:center;">'+
											            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">序號</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">貨品編號</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:6.5rem;">貨品名稱</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">單位</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">數量</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:3.5rem;">單價</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:2.5rem;">&nbsp;</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:2.5rem;">項計</td>'+
											            '<td style="border-right:1px solid #CFCFCF;width:11rem;">&nbsp;</td>'+
											          '</tr>';
											          if(orderRecordDetailList && Number(orderRecordDetailList.length)>0){
											          	  for(var k=0;k<orderRecordDetailList.length;k++){
												          	  var orderRecordDetail = orderRecordDetailList.item(k);
												          	  if(orderRecordDetail["OrdNumber"] == orderRecord["OrdNumber"]){
												          	  	  htmlStr +=  '<tr style="height:1.6rem;background-color:#E1E1E1;">'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["DetailNo"]+'</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["item"]+'</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["desc2"]+'</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["OrdUnit2"]+'</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["Qty"]+'</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">'+orderRecordDetail["UnitPrice2"]+'</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">$'+orderRecordDetail["Amt"]+'</td>'+
																	            '<td style="border-right:1px solid #CFCFCF;padding-left:0.1rem;">&nbsp;</td>'+
																	          '</tr>';
												          	  }
												          }
											          }
							        htmlStr +=  '</table>'+
								'</td>'+
							  '</tr>';
		            		}
						}
						htmlStr += 	'</table>';
		                Ext.getCmp("order-record-data").setHtml(htmlStr);
		                util.hideLoadMask();
					},1500);
				}
			}
		}
	}
});